package com.itheima.app.controller;

import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    UserManager userManager;

    @PostMapping("user/save")
    public Long save(@RequestBody User user){
        return userManager.save(user);
    }

    @GetMapping("user/findByMobile")
    public User findByMobile(String mobile){
        return userManager.findByMobile(mobile);
    }

    @PostMapping("/user/login")
    public void sendCode(@RequestBody Map<String,String> map){
        //1.获取手机号
        String phone = map.get("phone");
        //2.调用manager完成短信发送
        userManager.sendCode(phone);
    }

    @PostMapping("/user/loginVerification")
    public ResponseEntity regAndLogin(@RequestBody Map<String,String> map){
        return userManager.regAndLogin(map);
    }

    //更新用户信息
    @PostMapping("/user/loginReginfo")
    public ResponseEntity saveUserInfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token){
        return userManager.saveUserInfo(userInfo,token);
    }

    //更新用户头像
    @PostMapping({"/user/loginReginfo/head","/users/header"})
    public ResponseEntity saveUserInfoHead(MultipartFile headPhoto,@RequestHeader("Authorization") String token) throws IOException {
        return userManager.saveUserInfoHead(headPhoto,token);
    }

    //查询用户信息
    @GetMapping("/users")
    public ResponseEntity findUserInfoById(Long userID,@RequestHeader("Authorization") String token){
        //1.判断用户id是否为空
        UserInfoVo userInfoVo;
        if (userID!=null) {
            //1.1 若不为空就根据id查询
            userInfoVo = userManager.findUserInfoById(userID);
        }else{
            //1.2 若为空通过token获取用户id,根据id再进行查询
            User user = userManager.findUserFromToken(token);
            userInfoVo =  userManager.findUserInfoById(user.getId());
        }
        //2.返回userInfoVo
        return ResponseEntity.ok(userInfoVo);
    }

    @PutMapping("users")
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token){
        return userManager.updateUserInfo(userInfo,token);
    }
    //修改手机号- 1 发送短信验证码
    @PostMapping("/users/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(@RequestHeader("Authorization") String token){
        return userManager.sendVerificationCode(token);
    }

    //修改手机号 - 2 校验验证码
    @PostMapping("/users/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestHeader("Authorization") String token, @RequestBody Map<String, String> map) {
        String requestCode = map.get("verificationCode");
        return userManager.checkVerificationCode(requestCode, token);
    }
    //修改手机号 - 3 保存
    @PostMapping("/users/phone")
    public ResponseEntity savePhone(@RequestBody Map<String, String> map){
        String phone = map.get("phone");
        return userManager.savePhone(phone);
    }


    /**
     * 互相喜欢,喜欢,粉丝统计
     *
     * @return
     */
    @GetMapping("/users/counts")
    public ResponseEntity counts() {
        return userManager.counts();
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     *
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/users/friends/{type}")
    public ResponseEntity findByPage(
            @PathVariable Integer type,
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize
    ) {
        return userManager.findByPage(pageNum, pageSize, type);
    }

    /**
     * 粉丝-喜欢
     * @param userId
     */
    @PostMapping("/users/fans/{uid}")
    public void fans(@PathVariable("uid") String userId) {
        userManager.fans(userId);
    }

    /**
     * 粉丝 喜欢 - 取消
     *
     * @param userId
     */
    @DeleteMapping("/users/like/{uid}")
    public void unLike(@PathVariable("uid") String userId) {
        userManager.unLike(userId);
    }
}
